#!/usr/bin/perl -w

# ALL PURPOSE GENERAL PROGRAM
sub fixnum {
# num  - number to edit
# dig  - number of preceding digits
# dec  - number of decimal places
# type - one of
#      . r - regular integer
#      . z - zerofill
#      . p - leading plus or minus
my $debug = 0;


#Setup

   my ($onum,$odig,$odec,$type) = @_;   #SAVE ORIGINAL REQUEST
   if($onum =~ /[a-zA-Z]/) { 
     print STDERR "FIXNUM: $onum NOT A NUMBER\n";
     return $onum; 
   }
   my ($num,$dig,$dec) = ($onum,$odig,$odec); 
   if($debug) { print STDERR "start num: $num\n"; }
   if($debug) { print STDERR "  digits: $dig\n"; }
   if($debug) { print STDERR "  decimals: $dec\n"; }
   if($num) {
     $num =~ s/\s+$//; $num =~ s/^\s+//;
   }
   $type = substr($type,0,1);
   if($type eq 'z') { $spacer = "0"; }
   elsif($type eq 'p') { $spacer = "+"; }
   else { $spacer = " "; }
   if($debug) { print STDERR "  spacer: \'$spacer\'\n"; }
#   $num = int($num*(10**($dec+1))+0.5*signFunc($num))/(10**($dec+1));

#Start modifications
     #round
   $num = int($num*(10**$dec)+0.5*signFunc($num));   
     #copy to final product
   my $num2 = $num;
   if($debug) { print STDERR "  rounded: \'",$num2,"\'\n"; }
   $add = $dec + $dig - length($num);
   if($debug) { print STDERR "   need to add $add spacers\n"; }
   #if($num < 0) { 
   #  $add--;
   #   if($debug) { print STDERR "   neg: need to add $add spacers\n"; }
   #}
   if($add < 0) { #too many digits for this mode
        #add is negative, so really minus
      $dec+=$add;
        #re-round
      if($dec < 0) { $dec++; }
      $num = int($onum*(10**$dec)+0.5*signFunc($onum));
        #re-copy to final product
      $num2 = $num; #copy to final product
   } elsif($add > 0) { #not enough digits
      if($debug) { print STDERR "   adding $add spacers\n"; }
      while($add > 0) {
          #add a digit in front
        if($add >= $dig && $num >= 0) {
           $num2 = "0" . $num2; }
        elsif($add >= $dig-1 && $num < 0) {
          $num2 =~ s/-/0/; 
          $num2 = "-" . $num2;
        }
        else { $num2 = $spacer . $num2; }
        $add--;
      }
   }
   if($debug) { print STDERR "  after dig mod: \'",$num2,"\'\n"; }


#Make sure decimals are filled
   if($dec > 0) { 
      substr($num2,-1*$dec,0) .= ".";
   }
   if($debug) { print STDERR "  after dec mod: \'",$num2,"\'\n"; }

#SPECIAL CHECK FOR P-MODE
   if($type eq "p") {
     $num2 =~ s/\+\+/\ \+/g;
     $num2 =~ s/\+\+/\ \+/g;
     $num2 =~ s/\+\ /\ \ /g;
     $num2 =~ s/\+\-/\ \-/g;
   }

#RETURN!

   return $num2;
}


sub signFunc {
  my ($num) = @_;
  if($num && $num != 0) { 
    return $num/abs($num); 
  } else { 
    return 0;
  }
};

sub isint {
  my ($num) = @_;
  if($num && $num != 0) {
    if(int($num) == $num) {
      return 1;
    } else {
      return 0;
    }
  } 
  return 1;
}; 

#Old Function IDs

 sub fixnum10 { my ($num)=@_; fixnum($num,1,0,'r'); };
 sub fixnum11 { my ($num)=@_; fixnum($num,1,1,'r'); };
 sub fixnum12 { my ($num)=@_; fixnum($num,1,2,'r'); };
 sub fixnum13 { my ($num)=@_; fixnum($num,1,3,'r'); };
 sub fixnum14 { my ($num)=@_; fixnum($num,1,4,'r'); };
 sub fixnum15 { my ($num)=@_; fixnum($num,1,5,'r'); };
 sub fixnum16 { my ($num)=@_; fixnum($num,1,6,'r'); };
 sub fixnum17 { my ($num)=@_; fixnum($num,1,7,'r'); };

 sub fixnum20 { my ($num)=@_; fixnum($num,2,0,'r'); };
 sub fixnum21 { my ($num)=@_; fixnum($num,2,1,'r'); };
 sub fixnum22 { my ($num)=@_; fixnum($num,2,2,'r'); };
 sub fixnum23 { my ($num)=@_; fixnum($num,2,3,'r'); };
 sub fixnum24 { my ($num)=@_; fixnum($num,2,4,'r'); };
 sub fixnum25 { my ($num)=@_; fixnum($num,2,5,'r'); };
 sub fixnum26 { my ($num)=@_; fixnum($num,2,6,'r'); };

 sub fixnum30 { my ($num)=@_; fixnum($num,3,0,'r'); };
 sub fixnum31 { my ($num)=@_; fixnum($num,3,1,'r'); };
 sub fixnum32 { my ($num)=@_; fixnum($num,3,2,'r'); }; 
 sub fixnum33 { my ($num)=@_; fixnum($num,3,3,'r'); };
 sub fixnum34 { my ($num)=@_; fixnum($num,3,4,'r'); };
 sub fixnum35 { my ($num)=@_; fixnum($num,3,5,'r'); };

 sub fixnum40 { my ($num)=@_; fixnum($num,4,0,'r'); };
 sub fixnum41 { my ($num)=@_; fixnum($num,4,1,'r'); };
 sub fixnum42 { my ($num)=@_; fixnum($num,4,2,'r'); };
 sub fixnum43 { my ($num)=@_; fixnum($num,4,3,'r'); };
 sub fixnum44 { my ($num)=@_; fixnum($num,4,4,'r'); };
 sub fixnum45 { my ($num)=@_; fixnum($num,4,5,'r'); };
 sub fixnum46 { my ($num)=@_; fixnum($num,4,6,'r'); };

 sub fixnum50 { my ($num)=@_; fixnum($num,5,0,'r'); };
 sub fixnum51 { my ($num)=@_; fixnum($num,5,1,'r'); };
 sub fixnum52 { my ($num)=@_; fixnum($num,5,2,'r'); };
 sub fixnum53 { my ($num)=@_; fixnum($num,5,3,'r'); };
 sub fixnum54 { my ($num)=@_; fixnum($num,5,4,'r'); };
 sub fixnum55 { my ($num)=@_; fixnum($num,5,5,'r'); };



 sub fixnum20b { my ($num)=@_; fixnum($num,2,0,'z'); };
 sub fixnum21b { my ($num)=@_; fixnum($num,2,1,'z'); };
 sub fixnum22b { my ($num)=@_; fixnum($num,2,2,'z'); };
 sub fixnum23b { my ($num)=@_; fixnum($num,2,3,'z'); };
 sub fixnum24b { my ($num)=@_; fixnum($num,2,4,'z'); };
 sub fixnum25b { my ($num)=@_; fixnum($num,2,5,'z'); };

 sub fixnum30b { my ($num)=@_; fixnum($num,3,0,'z'); };
 sub fixnum31b { my ($num)=@_; fixnum($num,3,1,'z'); };
 sub fixnum32b { my ($num)=@_; fixnum($num,3,2,'z'); };
 sub fixnum33b { my ($num)=@_; fixnum($num,3,3,'z'); };
 sub fixnum34b { my ($num)=@_; fixnum($num,3,4,'z'); };
 sub fixnum35b { my ($num)=@_; fixnum($num,3,5,'z'); };

 sub fixnum31p { my ($num)=@_; fixnum($num,3,1,'p'); };

 sub fixnum40b { my ($num)=@_; fixnum($num,4,0,'z'); };
 sub fixnum41b { my ($num)=@_; fixnum($num,4,1,'z'); };
 sub fixnum42b { my ($num)=@_; fixnum($num,4,2,'z'); };
 sub fixnum43b { my ($num)=@_; fixnum($num,4,3,'z'); };
 sub fixnum44b { my ($num)=@_; fixnum($num,4,4,'z'); };
 sub fixnum45b { my ($num)=@_; fixnum($num,4,5,'z'); };

 sub fixnum50b { my ($num)=@_; fixnum($num,5,0,'z'); };
 sub fixnum51b { my ($num)=@_; fixnum($num,5,1,'z'); };
 sub fixnum52b { my ($num)=@_; fixnum($num,5,2,'z'); };
 sub fixnum53b { my ($num)=@_; fixnum($num,5,3,'z'); };
 sub fixnum54b { my ($num)=@_; fixnum($num,5,4,'z'); };
 sub fixnum55b { my ($num)=@_; fixnum($num,5,5,'z'); };

 sub fixnum60b { my ($num)=@_; fixnum($num,6,0,'z'); };
 sub fixnum90b { my ($num)=@_; fixnum($num,9,0,'z'); };
 
return 1;


#CHANGELOG
# 11-26-03 added functionality to add space corresponding to
#          requested size
